package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadALOSSController {

//    本地存储
    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
        log.info("文件上传,username：{},age:{},image：{}",username,age,image);    //单个文件默认上传大小为1M

        //将获取的文件存储在C盘的下载目录下
        String originalFilename= image.getOriginalFilename();
        int index=originalFilename.lastIndexOf(".");
        String extname=originalFilename.substring(index);
        String newFileName= UUID.randomUUID().toString()+extname;   //uuid：通用唯一识别码
        log.info("新的文件名：{}",newFileName);
        image.transferTo(new File("C:\\Users\\a1592\\Downloads\\"+newFileName));

        return Result.success();
    }*/

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public String upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名：{}",image.getOriginalFilename());

        //调用阿里云OSS工具类进行文件上传
        String url= aliOSSUtils.upload(image);
        log.info("文件上传成功，文件访问的url：{}",url);

        return url;
    }
}
